<%-- 
    Document   : staff
    Created on : Jun 21, 2012, 11:18:25 AM
    Author     : Home
--%>
<%@page import="org.omg.PortableInterceptor.SYSTEM_EXCEPTION"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="dbTool.functions"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="../css1/styles.css" rel="stylesheet" type="text/css" />
        <script src="../js/jquery.min.js" type="text/javascript"></script>
        <script src="../js/dropmenu.js" type="text/javascript"></script>

        <script type="text/javascript" src="../js/jquery-1.2.6.min.js"></script>
        <script type="text/javascript" src="../js/jquery-ui-personalized-1.5.2.packed.js"></script>
        <script type="text/javascript" src="../js/sprinkle.js"></script>
        <title>Staff</title>
        <script language = "Javascript">
            function Validate(){
                var user=document.f1.user;
                var pass=document.f1.pass;
                var dealerID=document.f1.dealerID;
                var fname=document.f1.fname;
                var role=document.f1.role;
                var gender=document.f1.gender;
                var bday=document.f1.bday;
                var address=document.f1.address;
                var phone=document.f1.phone;
                var mail=document.f1.mail;
                var status=document.f1.status;
                
                if ((user.value==null)||(user.value=="")){
                    alert("Please enter User Name")
                    user.focus()
                    return false
                }
                
                if ((pass.value==null)||(pass.value=="")){
                    alert("Please enter Password")
                    pass.focus()
                    return false
                }
                
                if((dealerID.value==null)||(dealer.value=="")){
                    alert("Please choise DealerID")
                    dealerID.focus()
                    return false
                }
                
                if ((fname.value==null)||(fname.value=="")){
                    alert("Please enter Full Name")
                    fname.focus()
                    return false
                }
                
                if ((role.value==null)||(role.value=="")){
                    alert("Please choise Role")
                    role.focus()
                    return false
                }
                
                if ((gender.value==null)||(gender.value=="")){
                    alert("Please enter Gender")
                    gender.focus()
                    return false
                }
                
                if ((bday.value==null)||(bday.value=="")){
                    alert("Please enter Birthday")
                    bday.focus()
                    return false
                }
                
                if ((address.value==null)||(address.value=="")){
                    alert("Please enter Address")
                    address.focus()
                    return false
                }
                
                if ((phone.value==null)||(phone.value=="")){
                    alert("Please enter Phone Number")
                    phone.focus()
                    return false
                }
                
                var reg = /\d{10}/;
                if(!phone.value.match(reg))
                {
                    alert(phone.value+ " does not contain a valid code!");
                    phone.focus()
                    return false;
                }
                
                if ((mail.value==null)||(mail.value=="")){
                    alert("Please enter Mail")
                    mail.focus()
                    return false
                }
                
                if ((status.value==null)||(status.value=="")){
                    alert("Please enter Status")
                    status.focus()
                    return false
                }
                
            }
            
            function Delete() { 
                if (confirm("Are you sure you want to delete this Staff?")) { 
                    return true; 
                }else{ 
                    return false; 
                } 
            } 
        </script>
    </head>
    <body>
        <%
            String user = (String) session.getAttribute("username");
            if (user == null) {
                session.setAttribute("error", "Please login!");
                response.sendRedirect("login.jsp");
            }
        %>
        <jsp:include page="banner.jsp"></jsp:include>
            <div id="leftcolumn">
            <jsp:include page="menuleft.jsp"></jsp:include>            
            </div>
            <div id="content">
            <%
                functions f = new functions();
                ResultSet r = f.viewDealer();
                ResultSet rs = f.viewStaff();
            %>
            <form name="f2" action="searchStaff.jsp" method="post">
                <input name="search" type="text"/>
                <input type="submit" value="Search"/>
                <span style="color: green"> Input <i> name</i> or <i>id</i> or <i>phone number</i> to search</span>
            </form>
            <br />
            <table style="text-align: center" width="100%" name="staff-view-table" class="staff-view-table">
                <tr>
                    <th>Staff account</th>
                    <th>DealerID</th>
                    <th>Full name</th>
                    <th>Role</th>
                    <th>Gender</th>
                    <th>Birthday</th>                    
                    <th>Address</th>
                    <th>Phone</th>
                    <th>Email</th>
                    <th>Status</th>
                    <th>Update</th>
                </tr> 
                <%
                    String s;
                    while (rs.next()) {
                        s = rs.getInt("id") + "";
                        String sA = rs.getString("staffAcc");
                        String dI = rs.getString("dealerID");
                        String full = rs.getString("fullName");
                        int i = rs.getInt("role");
                        boolean gender = rs.getBoolean("gender");
                        String bd = rs.getString("birthday");
                        String addr = rs.getString("address");
                        String ph = rs.getString("phone");
                        String ma = rs.getString("mail");
                        boolean status = rs.getBoolean("status");
                %>
                <tr>
                    <td> <%= sA%></td>
                    <td> <%= dI%></td>
                    <td> <%= full%></td>
                    <td>                         
                        <%
                            if (i == 1) {
                                out.print("Staff 1");
                            }
                            if (i == 2) {
                                out.print("Staff 2");
                            }
                            if (i == 3) {
                                out.print("Staff 3");
                            }
                        %> 
                    </td>
                    <td>
                        <%

                            if (gender) {
                                out.print("Male");
                            } else {
                                out.print("Female");
                            }
                        %>    
                    </td>
                    <td> <%=bd%> </td>
                    <td> <%=addr%> </td>
                    <td> <%=ph%> </td>
                    <td> <%=ma%> </td>
                    <td>
                        <%

                            if (status) {
                                out.print("Enable");
                            } else {
                                out.print("Disable");
                            }
                        %>
                    </td>
                    <td>                        
                        <a class="edit" href="staff.jsp?id=<%=s%>">Edit</a>  
                    </td>



                </tr>
                <%
                    }
                %>
            </table>
            <br />
            <%

                String sID = request.getParameter("id");
                if (sID == null) {%>
            <form name="f1" action="addStaff.jsp" method="post" onSubmit="return Validate()">

                <table style="width: auto;text-align: left;">
                    <tr>
                        <td>Dealer name:</td>
                        <td>
                            <select name="dealerID">
                                <%
                                    while (r.next()) {
                                        String dID = r.getString("dealerID");
                                        String dName = r.getString("dealerName");
                                        out.print("<option value='" + dID + "'>" + dName + "</option>");
                                    }
                                %>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Staff account:</td>
                        <td><input type="text" name="sAcc"/></td>
                    </tr>
                    <tr>
                        <td>Staff name:</td>
                        <td><input type="text" name="sName"/></td>
                    </tr>                    
                    <tr>
                        <td>Role:</td>
                        <td>
                            <select name="role">
                                <option value="1">Staff 1</option>
                                <option value="2">Staff 2</option>
                                <option value="3">Staff 3</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Gender:</td>
                        <td>
                            <select name="gender">
                                <option value="1">Male</option>
                                <option value="0">Female</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Birthday(MM/dd/yyyy):</td>
                        <td><input type="text" name="bday"/></td>
                    </tr>
                    <tr>
                        <td>Address:</td>
                        <td>
                            <textarea name="address" rows="4"></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td>Phone Number:</td>
                        <td><input type="text" name="phone"/></td>
                    </tr>
                    <tr>
                        <td>Email:</td>
                        <td><input type="text" name="mail"/></td>
                    </tr>                    
                    <tr>
                        <td>Status:</td>
                        <td>
                            <select name="status">
                                <option value="1">Enable</option>
                                <option value="0">Disable</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><input type="submit" name="cmd" value="Add Staff"/></td>
                    </tr>
                </table>
            </form>
            <%} else {
                int id1 = Integer.parseInt(request.getParameter("id"));
                String sid1 = id1 + "";
                ResultSet rs1 = f.viewStaffByID(id1);
                ResultSet r1 = f.viewDealer();

                rs1.next();
                String staffID = rs1.getInt("id") + "";

                String account = rs1.getString("staffAcc");

            %>
            <form name="f1" action="addStaff.jsp?id=<%= sid1%>" method="post" onSubmit="return Validate()">
                <table style="text-align: left; " align="center" class="edited-table" cellpadding="8" width="80%" >
                    <tr>
                        <td>Dealer name:</td>
                        <td>
                            <select name="dealerID">
                                <%
                                    String dealerID1 = rs1.getString("dealerID");
                                    while (r1.next()) {
                                        String s1 = r1.getString("dealerID");
                                        String s2 = r1.getString("dealerName");
                                        if (s1.equals(dealerID1)) {
                                            out.print("<option value='" + s1 + "' selected='selected'>" + s2 + "</option>");
                                        } else {
                                            out.print("<option value='" + s1 + "'>" + s2 + "</option>");
                                        }
                                    }
                                %>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Staff account:</td>
                        <td><input type="text" name="sAcc" readonly="true" value="<%= account%>"/></td>
                    </tr>
                    <tr>
                        <td>Staff name:</td>
                        <%

                            String name = rs1.getString("fullName");
                        %>
                        <td><input type="text" name="sName" value="<%= name%>"/></td>
                    </tr>                    
                    <tr>
                        <td>Role:</td>
                        <td>
                            <select name="role">
                                <%
                                    int role = rs1.getInt("role");
                                    if (role == 1) {
                                        out.print("<option value='1' selected='selected'>Staff 1</option><option value='2'>Staff 2</option><option value='3'>Staff 3</option>");
                                    } else if (role == 2) {
                                        out.print("<option value='1'>Staff 1</option><option value='2' selected='selected'>Staff 2</option><option value='3'>Staff 3</option>");
                                    } else {
                                        out.print("<option value='1'>Staff 1</option><option value='2'>Staff 2</option><option value='3' selected='selected'>Staff 3</option>");
                                    }
                                %>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Gender:</td>
                        <td>
                            <select name="gender">
                                <%

                                    boolean gender = rs1.getBoolean("gender");
                                    if (gender) {
                                        out.print("<option value='1' selected='selected'>Male</option><option value='0'>Female</option>");
                                    } else {
                                        out.print("<option value='1'>Male</option><option value='0' selected='selected'>Female</option>");
                                    }
                                %>
                            </select>

                        </td>
                    </tr>
                    <tr>
                        <td>Birthday(MM/dd/yyyy):</td>
                        <%
                            String bday = rs1.getString("birthday");
                        %>
                        <td><input type="text" name="bday" value="<%= bday%>"/></td>
                    </tr>
                    <tr>
                        <td>Address:</td>
                        <td>
                            <%
                                String address = rs1.getString("address");
                            %>
                            <textarea name="address" rows="4"><%= address%></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td>Phone Number:</td>
                        <%
                            String phone = rs1.getString("phone");
                        %>
                        <td><input type="text" name="phone" value="<%= phone%>"/></td>
                    </tr>
                    <tr>
                        <td>Email:</td>
                        <%
                            String mail = rs1.getString("mail");
                        %>
                        <td><input type="text" name="mail" value="<%= mail%>"/></td>
                    </tr>                    
                    <tr>
                        <td>Status:</td>
                        <td>
                            <select name="status">
                                <%

                                    boolean status = rs1.getBoolean("status");
                                    if (status) {
                                        out.print("<option value='1' selected='selected'>Enable</option><option value='0'>Disable</option>");
                                    } else {
                                        out.print("<option value='1'>Enable</option><option value='0' selected='selected'>Disable</option>");
                                    }
                                %>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td style="float: right"><input type="submit" name="cmd" value="Update Staff"/></td>
                        <td>
                            <a class="edit" href="resetPass.jsp?id=<%= staffID%>">Reset Pass</a>
                        </td>
                    </tr>
                </table>
            </form>
            <%
                }
            %>

        </div>
    </body>
</html>
